package com.car.advertising.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.car.advertising.CarApplication;
import com.car.advertising.R;
import com.car.advertising.activity.adapter.TaskRecordAdapter;
import com.car.advertising.connection.HttpUrl;
import com.car.advertising.entity.TaskRecordEntity;
import com.support.widget.JumpManager;
import com.support.widget.layout.BaseRecylerViewToolBarActivity;
import com.support.widget.network.OkHttpClientManager;
import com.support.widget.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class TaskRecordActivity extends BaseRecylerViewToolBarActivity {
    private TaskRecordAdapter adapter;
    private List<TaskRecordEntity> taskRecordList = new ArrayList<>();

    @Override
    protected void initToolBar() {
        setToolBarBackgroundColor(R.color.color_white);
        setToolBarTitle(getString(R.string.task_record_title));
        setToolBarTitleColor(R.color.black_bg);
    }

    @Override
    protected void init() {
        setPullDownEnabled(false);
        setPullUpEnabled(false);
        initdatas();
        getDetailsData();
    }

    private void initdatas() {
        if (adapter == null) {
            adapter = new TaskRecordAdapter(this, taskRecordList);
            initRecyclerView(adapter);
            adapter.setLookDetailClick(lookDetailClick);
        } else {
            adapter.setRefreshList(taskRecordList);
        }
    }

    private TaskRecordAdapter.LookDetailClick lookDetailClick = new TaskRecordAdapter.LookDetailClick() {

        @Override
        public void onLookClick(int position) {
            String taskStatus = taskRecordList.get(position).getTaskStatus();
            if (TextUtils.isEmpty(taskStatus)) {
                return;
            }
            if (TextUtils.equals(taskStatus, "1")) {
                JumpManager.gotoActivity(TaskRecordActivity.this, TaskAttendanceActivity.class, false);
            } else {
                ToastUtil.show(TaskRecordActivity.this, position + "--------查看详情-------", Toast.LENGTH_SHORT);
            }
        }
    };

    private void getDetailsData() {
        List<OkHttpClientManager.Param> params = new ArrayList<>();
        params.add(new OkHttpClientManager.Param("acToken", CarApplication.token));
        OkHttpClientManager.postAsyn(HttpUrl.getInstance().URL_038, new OkHttpClientManager.ResultCallback<List<TaskRecordEntity>>() {
            @Override
            public void onError(String request) {
                if (TextUtils.equals(request, "登录已过期")) {
                    JumpManager.gotoActivity(TaskRecordActivity.this, LoginActivity.class, false);
                    finish();
                } else {
                    ToastUtil.show(TaskRecordActivity.this, request, Toast.LENGTH_SHORT);
                }
            }

            @Override
            public void onResponse(List<TaskRecordEntity> resp) {
                if (resp == null || resp.size() == 0) {
                    return;
                }
                taskRecordList = resp;
                initdatas();
            }

            @Override
            public void onFinish() {
            }
        }, params, null);
    }

    @Override
    protected boolean isClickable() {
        return false;
    }

    @Override
    protected void subtitleClick() {

    }

    @Override
    protected void titleClick() {

    }

    @Override
    protected void rightClick() {

    }

    @Override
    protected void onRefreshFinish() {

    }

    @Override
    protected void onLoadMoreFinish() {

    }


    @Override
    public String getCN() {
        return null;
    }

    @Override
    public void LogoutCallback(boolean islogin) {

    }

    @Override
    public void onCritClick(View v) {

    }
}
